<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item>
          <el-select
            clearable
            v-model="form.merchant_id"
            placeholder="商户"
            filterable
          >
            <el-option
              v-for="item in payMerchantList"
              :key="item.value"
              :label="item.name + '-' + item.merchant_id"
              :value="item.merchant_id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item>
          <el-input v-model="form.merchant_id" placeholder="商户ID" />
        </el-form-item> -->
        <el-form-item>
          <el-input v-model="form.base_currency" placeholder="基础币种" />
        </el-form-item>
        <el-form-item>
          <el-input v-model="form.quote_currency" placeholder="目标币种" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="danger" @click="onAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="merchant_id" label="商户ID" align="center" />
          <el-table-column
            prop="merchant_name"
            label="商户名称"
            align="center"
          />
          <el-table-column
            prop="base_currency"
            label="基准币种"
            align="center"
          />
          <el-table-column
            prop="quote_currency"
            label="目标币种"
            align="center"
          />
          <el-table-column prop="price" label="目标币种价格" align="center" />
          <el-table-column
            prop="expire_time"
            label="价格过期时间"
            align="center"
            :formatter="formatterTime"
          />
          <el-table-column
            prop="createtime"
            label="创建时间"
            align="center"
            :formatter="formatterTime"
          />
          <el-table-column
            label="操作"
            align="center"
            fixed="right"
            width="150"
          >
            <template #default="scope">
              <el-button
                type="primary"
                plain
                @click="handleOperate(scope.$index, scope.row, 'edit')"
                >编辑价格</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      :dialogVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
      :dialogTitle="dialogTitle"
      :merchantList="payMerchantList"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive, computed } from "vue";
import { formatDates, formatterTime, timestamp } from "@/utils/utils";
import {
  exchangeMerchantRateApi,
  exchangeMerchantRateEditApi,
} from "@/api/payServerse/exchange";
import DialogForm from "./components/dialogForm.vue";
import { GlobalStore } from "@/store";

const globalStore = GlobalStore();
const payMerchantList = computed(() => globalStore.payMerchantList);

const dialogVisible = ref(false);
const dialogTitle = ref("");
const btnLoading = ref(false);
const btnType = ref(null);
const loading = ref(false);
const tableData = ref([]);
const merchantList = ref([]);

const formData = {
  base_currency: "",
  quote_currency: "",
  merchant_id: "",
};
const form = reactive({ ...formData });
// 新增、编辑参数
const dialogFormData = {
  id: "",
  merchant_id: "",
  price: "",
  base_currency: "",
  quote_currency: "",
  expire_time: "",
};
const dialogForm = reactive({ ...dialogFormData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    merchant_id: form.merchant_id,
    base_currency: form.base_currency,
    quote_currency: form.quote_currency,
  };
};

const onAdd = async () => {
  btnType.value = "add";
  dialogForm.id = "";
  dialogVisible.value = true;
  dialogTitle.value = "新增商户币种汇率";
};

// 行操作
const handleOperate = async (index, row, type) => {
  // 编辑
  if (type === "edit") {
    btnType.value = "edit";
    Object.assign(dialogForm, { ...row });
    dialogForm.expire_time = formatDates(dialogForm.expire_time);
    dialogVisible.value = true;
    dialogTitle.value = "编辑商户币种汇率";
  }
};
// 提交
const submit = async (param) => {
  try {
    btnLoading.value = true;
    const res = await exchangeMerchantRateEditApi(param);
    ElMessage({
      message: res.message,
      type: "success",
    });
    dialogVisible.value = false;
    btnLoading.value = false;
  } catch {
    dialogVisible.value = false;
    btnLoading.value = false;
  }
  getTbaleData();
};
// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await exchangeMerchantRateApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
//关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, { ...dialogFormData });
};
// 取消
const cancel = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, { ...dialogFormData });
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
  getTbaleData();
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
